package com.library.uiframework.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.library.uiframework.R;
import com.library.uiframework.fragment.BaseFragment;

/**
 * 在此写用途
 *
 * @version V1.0 <描述当前版本功能>
 * @FileName: com.youxue.listener.activity.WrapFragmentActivity.java
 * @author: zhiwei
 * @date: 2016-04-02 17:57
 */
public class WrapActionBarFragmentActivity<T extends BaseFragment> extends ActionBarLayoutActivity {
    T fragment;
    /**
     * 嵌套的fragment完整class name
     */
    public static final String KEY_CLASS_NAME = "key_class_name";
    public static final String KEY_ACTION_BAR_TITLE = "ACTION_BAR_TITLE";

    /**
     * @return 包装的是哪个fragment
     */
    protected Class wrapFragmentClass() {
        return null;
    }

    protected T getWrapFragment() {
        return (T)fragment;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    public static void startSelf(Context context, Class clazz, Bundle bundle){
        Intent intent = new Intent(context, WrapActionBarFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void startSelf(Activity context, Class clazz, Bundle bundle){
        Intent intent = new Intent(context, WrapActionBarFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    public static void startSelf(Fragment fragment, Class clazz, Bundle bundle){
        Intent intent = new Intent(fragment.getActivity(), WrapActionBarFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        fragment.startActivity(intent);
    }

    public static void startSelf(android.app.Fragment fragment, Class clazz, Bundle bundle){
        Intent intent = new Intent(fragment.getActivity(), WrapActionBarFragmentActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getCanonicalName());
        if(bundle != null){
            intent.putExtras(bundle);
        }
        fragment.startActivity(intent);
    }

    @Override
    protected View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.common_activity_actionbar_empty, container, false);
        initAcivity();
        return view;
    }

    private void initAcivity(){
        Class clazz = null;

        final Intent intent = getIntent();
        String title = intent.getStringExtra(KEY_ACTION_BAR_TITLE);
        setTitle(title);
        if (intent.hasExtra(KEY_CLASS_NAME)) {
            try {

                clazz = Class.forName(intent.getStringExtra(KEY_CLASS_NAME));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (clazz == null) {
            clazz = wrapFragmentClass();
        }
        if (clazz != null) {
            try {
                fragment = (T) clazz.newInstance();
                fragment.setArguments(intent.getExtras());
                launchRootFragment(fragment, clazz.getName());
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("", this.getClass().getSimpleName() + "Error: 嵌套的class类型,必须是BaseFragment");
                finish();
            }
        } else {
            Log.d("", this.getClass().getSimpleName() + "Error: 请设置KEY_CLASS_NAME或者实现wrap");
            finish();
        }
    }
}
